package com.tourism.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tourism.dao.OrderInfoDao;
import com.tourism.dao.UserDao;
import com.tourism.entity.OrderInfo;
import com.tourism.entity.OrderPayInfo;
import com.tourism.entity.OrderQuery;
import com.tourism.entity.User;
import com.tourism.service.OrderInfoService;
import com.tourism.util.SystemHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description
 * @Author z
 * @Date 2021-03-01 21:05
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private OrderInfoDao orderInfoDao;


    @Override
    public OrderInfo getOrderInfoById(Long id) {
        OrderInfo orderInfo = orderInfoDao.getOrderInfoById(id);
        User user = userDao.getUserById(orderInfo.getOrderUser());
        orderInfo.setOrderUserName(user.getUsername());
        return orderInfo;
    }
//删除线路订单
    @Override
    public Boolean deleteOrderInfoById(Long id) {
        return orderInfoDao.deleteOrderInfoById(id);
    }

//新增景点订单
    @Override
    public boolean addOrderInfo(OrderInfo orderInfo) {
        orderInfo.init();
        orderInfo.setOrderUser(SystemHelper.getUserId());
        return orderInfoDao.addOrderInfo(orderInfo);
    }


    @Override
    public PageInfo<OrderInfo> getOrderInfoList(OrderQuery orderQuery) {
        PageHelper.startPage(orderQuery.getPageNum(), orderQuery.getPageSize());
//        返回1当前登入1用户
        User currentUser = SystemHelper.getCurrentUser();
        //不是管理员，查询自己的
        if (!SystemHelper.isAdmin()) {
            orderQuery.setOrderUser(currentUser.getId());
        }
        List<OrderInfo> OrderInfoList = orderInfoDao.getOrderInfoList(orderQuery);
        PageInfo<OrderInfo> result = new PageInfo<>(OrderInfoList);
        return result;
    }

    @Override
    public boolean updateOrderPayInfo(OrderPayInfo orderPayInfo) {
        orderPayInfo.init();
        return orderInfoDao.updateOrderPayInfo(orderPayInfo);
    }


}
